class UserLogosController < ApplicationController
  layout 'base.html',:only => [:new, :edit, :show,:create]
  caches_page :show
  
  def new
    @logo = UserLogo.new(:user_id => current_user_id)
    @seo_title = "麦吐司网-设置新头像"
  end
  
  def create
    @logo = UserLogo.new(params[:user_logo].merge(:user_id => current_user_id))
    @logo.save ? redirect_to(:action => :edit, :id => @logo.id) : render(:new)
  end
  
  def edit
    @logo = UserLogo.find(params[:id])
    @seo_title = "麦吐司网-头像裁剪"  
  end
  
  def update
    @source = UserLogo.find(params[:id])
    @logo = UserLogo.new(
      :user_id => current_user_id,
      :image_file_url => (Rails.env.production? ? "http://l.mytusi.com#{user_logo_path(@source,:jpg)}" : user_logo_url(@source,:jpg))
    )
    instance_eval( "
    @logo.operate do |image|
      image.crop(        
        :from => '#{params[:x1]}x#{params[:y1]}',
        :size => '#{params[:width]}x#{params[:height]}'
      )
    end ")
    @logo.save ? redirect_to(user_url current_user_id) : render(:edit)    
  end
  
  def show
    @logo = UserLogo.find(params[:id])
  end
end
